﻿IF EXISTS (SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = 'OTParametrosGet')
	BEGIN
		DROP  Procedure  OTParametrosGet
	END
GO


CREATE PROCEDURE OTParametrosGet
	(	
		@EmpresaId	AS INT,
		@SucursalId	AS INT
	)	
AS
BEGIN	
	SET NOCOUNT ON

	-- Lista de tablas a ser devueltas a la aplicacion para cargar la hoja de registro	
	SELECT 0 AS TableId, 'Catalog'		AS TableName	UNION
	SELECT 1 AS TableId, 'Producto'		AS TableName	UNION
	SELECT 2 AS TableId, 'Material'		AS TableName	UNION
	SELECT 3 AS TableId, 'Operario'		AS TableName    UNION
	SELECT 4 AS TableId, 'Maquina'		AS TableName    UNION		
	SELECT 5 AS TableId, 'Gasto'		AS TableName    --UNION
	
	-- Producto
	SELECT	A.art_id AS Id, art_nombre AS Nombre
	FROM	Articulo A
	INNER JOIN Articulo_x_Bodega AB 
		ON	A.emp_id = AB.emp_id 
		AND A.art_id = AB.art_id 	
	INNER JOIN lit_parametrosbodega P
		ON  P.EmpresaId = AB.Emp_id
		AND P.SucursalId = AB.Suc_id
		AND P.BodegaProductoTerminado = AB.Bod_id
	WHERE P.EmpresaId = @EmpresaId
	  AND P.SucursalId = @SucursalId	 	  
	ORDER BY 2  

	-- Materia Prima
	EXEC materialGetList @EmpresaId, @SucursalId
	 
	-- Operario
	SELECT	Id, Nombre
	FROM	Lit_Operario
	WHERE	EmpresaId = @EmpresaId

	-- Maquina
	SELECT	Id, Nombre
	FROM	Lit_Maquina
	WHERE	EmpresaId = @EmpresaId

	-- Gasto 
	Exec GastoGet @EmpresaId, NULL
	
	SET NOCOUNT OFF
END

GO